// JavaScript Document
$(document).ready(init);

var speed=500;

function init(){
	
	window.onload=function(){
		$("#carousel ul li img").each(function(){zoomImage(60,60,70,70,this);})
		zoomImage(300,267,320,287,$("#largeImg img").get(0));
	}	
	
	initList();
	
	addListener();
}
function addListener(){	
	$("#carousel li").click(openLarge);
	$("#carousel .right").click(scrollRight);
	$("#carousel .left").click(scrollLeft);
	
}
function initList(){
	var num=$("#carousel li.conon").prevAll().length-1;
	beginLeft=-80*num;
	$("#carousel ul").css("left",beginLeft + "px");
}
function scrollRight(){
	var num=$("#carousel li").length-3;
	if(beginLeft<=(-80*num)) return;
	beginLeft=beginLeft-80;
	$("#carousel ul").animate({left:beginLeft},speed);
	
	var center=$("#carousel li.conon").get(0);
	$(center).next().addClass("conon");
	$(center).removeClass("conon");
}
function scrollLeft(){
	if(beginLeft>=80) return;
	beginLeft=beginLeft+80;
	$("#carousel ul").animate({left:beginLeft},speed);
	
	var center=$("#carousel li.conon").get(0);
	$(center).prev().addClass("conon");
	$(center).removeClass("conon");
}
function openLarge(){
	var ctrl =null;
	try{
		ctrl=getBusyOverlay($("#largeImg").get(0));
	}catch(err){
		//alert("\u6D4F\u89C8\u5668\u8FD8\u517C\u5BB9\uFF0C\u90E8\u5206\u6548\u679C\u65E0\u6CD5\u6B63\u5E38\u663E\u793A\uFF01");
	}
	var imgId = $(this).find("img").get(0).id;
	$.ajax({url:"/testIphoneUI/test11.html",
	//$.ajax({url:"/tools/ajax.aspx?imgId=" + imgId,
		   	dataType:"json",
			success:function(dataObj){
						updateLargeImg(dataObj.src,dataObj.description);						
						if(ctrl!=null) {ctrl.remove();}
						addComment(dataObj.comments,dataObj.commentNum);	
					}})
	changePhoto(this);
}
function changePhoto(target){
	if($(target).next().hasClass("conon")){
		scrollLeft();
	}
	else if($(target).prev().hasClass("conon")){
		scrollRight();	
	}
}
function updateLargeImg(src,title){
	var imgObj=$("#largeImg img").get(0);
	imgObj.src=src;
	imgObj.onload=function(){
		zoomImage(300,267,320,287,imgObj);
	}
	$("#largeImg .contitle").html(title);
}
function addComment(comments,num){
	$("#commentNum").html(num);
	$("#pinglun").html("");
	for(var i=0;i<comments.length;i++){
		var html='<div class="user_list">'
      			+ '<a  href="#" class="list_left"><img src="' + comments[i].face + '" /></a>'
  				+ '<a href="#" class="list_right">'
				+ '<div class="mlt_left"></div>'
				+ '<div class="mlt_c">'
				+ '<div class="ww"></div>'
				+ '<div class="title1"><div class="name">' + comments[i].nickname + '</div>'
				+ '<div class="time" style="color:#FF8601;">' + comments[i].publishTime + '</div></div>'
				+ '<div class="kb"></div>'
				+ '<div class="content"><div class="confont">' + comments[i].context + '</div></div>'
				+ '<div class="fff"></div></div></a></div>';
		$("#pinglun").append(html);
	}
}
function zoomImage(width,height,outWidth,outHeight,imgObj){
	var img=new Image();
	img.src=imgObj.src;
	var xscale=width<img.width?width/img.width:1
	var yscale=height<img.height?height/img.height:1;
	var scale=xscale<yscale?xscale:yscale;
	imgObj.style.cssText="height:" + img.height*scale
						+ "px;width:" + img.width*scale
						+ "px;margin:" + (outHeight-img.height*scale)/2
						+ "px " + (outWidth-img.width*scale)/2 +"px;";	
}

function gotoMorePage(url){
	var btnArray=[{name:'YES',text:'确 定',def:true},{name:'NO',text:'取 消'}]
	var box=new alertBox("页面跳转，是否执行跳转页面？",null,btnArray,true);
	box.YES.onclick=function(){window.location.href=url;}
	box.NO.onclick=function(){box.hide()}	
}
